home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / By the Book / Mac C Primer V1 / Primer V1 Projects (T7) / 5.1 - WorldClock / WorldClock.c next >
Encoding:
C/C++ Source or Header  |  1994-05-13  |  10.8 KB  |  327 lines  |  [TEXT/KAHL]

  1. /********************************************************/
  2. /*                                                        */
  3. /*  WorldClock Code from Chapter Five of                */
  4. /*                                                        */
  5. /*    *** The Macintosh Programming Primer, 2nd Ed. ***    */
  6. /*                                                      */
  7. /*    Copyright 1992, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /*  This program demonstrates specific Mac programming    */
  10. /*    techniques.                                            */
  11. /*                                                        */    
  12. /*    See the WindowMaker project for a more complete        */
  13. /*    example of a standalone Mac application.              */
  14. /*                                                        */
  15. /********************************************************/
  16.  
  17. #include <Packages.h>
  18. #include <GestaltEqu.h>
  19.  
  20. #define kBaseResID            128
  21. #define kMoveToFront        (WindowPtr)-1L
  22. #define kUseDefaultProc        (void *)-1L
  23. #define kSleep                20L
  24. #define kLeaveWhereItIs        false
  25.  
  26. #define kIncludeSeconds        true
  27. #define kTicksPerSecond        60
  28. #define kSecondsPerHour        3600L
  29.  
  30. #define kAddCheckMark        true
  31. #define kRemoveCheckMark    false
  32.  
  33. #define kPopupControlID        kBaseResID
  34.  
  35. #define kNotANormalMenu        -1
  36.  
  37. #define mApple                kBaseResID
  38. #define iAbout                1
  39.  
  40. #define mFile                kBaseResID+1
  41. #define iQuit                1
  42.  
  43. #define mFont                100
  44.  
  45. #define mStyle                101
  46. #define iPlain                1
  47. #define iBold                2
  48. #define iItalic                3
  49. #define iUnderline            4
  50. #define iOutline            5
  51. #define iShadow                6
  52.  
  53. #define kPlainStyle            0
  54.  
  55. #define kExtraPopupPixels    25
  56.  
  57. #define kClockLeft            12
  58. #define kClockTop            25
  59. #define kClockSize            24
  60.  
  61. #define kCurrentTimeZone    1
  62. #define kNewYorkTimeZone    2
  63. #define kMoscowTimeZone        3
  64. #define kUlanBatorTimeZone    4
  65.  
  66. #define TopLeft( r )        (*(Point *) &(r).top)
  67. #define BottomRight( r )    (*(Point *) &(r).bottom)
  68.  
  69. #define IsHighBitSet( longNum )        ( (longNum >> 23) & 1 )
  70. #define SetHighByte( longNum )        ( longNum |= 0xFF000000 )
  71. #define ClearHighByte( longNum )    ( longNum &= 0x00FFFFFF )
  72.  
  73.  
  74. /*************/
  75. /*  Globals  */
  76. /*************/
  77.  
  78. Boolean         gDone, gHasPopupControl;
  79. short        gLastFont = 1, gCurrentZoneID = kCurrentTimeZone;
  80. Style        gCurrentStyle = kPlainStyle;
  81. Rect        gClockRect;
  82.  
  83.  
  84. /***************/
  85. /*  Functions  */
  86. /***************/
  87.  
  88. void    ToolBoxInit( void );
  89. void    WindowInit( void );
  90. void    MenuBarInit( void );
  91. void    EventLoop( void );
  92. void    DoEvent( EventRecord *eventPtr );
  93. void     HandleNull( EventRecord *eventPtr );
  94. void    HandleMouseDown( EventRecord *eventPtr );
  95. void    SetUpZoomPosition( WindowPtr window, short zoomInOrOut );
  96. void    HandleMenuChoice( long menuChoice );
  97. void    HandleAppleChoice( short item );
  98. void    HandleFileChoice( short item );
  99. void    HandleFontChoice( short item );
  100. void    HandleStyleChoice( short item );
  101. void    DoUpdate( EventRecord *eventPtr );
  102. long    GetZoneOffset( void );
  103.  
  104.  
  105. /**************************** main **********************/
  106.  
  107. void    main( void )
  108. {
  109.     ToolBoxInit();
  110.     WindowInit();
  111.     MenuBarInit();
  112.     
  113.     EventLoop();
  114. }
  115.  
  116.  
  117. /****************** ToolBoxInit *********************/
  118.  
  119. void    ToolBoxInit( void )
  120. {
  121.     InitGraf( &qd.thePort );
  122.     InitFonts();
  123.     InitWindows();
  124.     InitMenus();
  125.     TEInit();
  126.     InitDialogs( 0L );
  127.     InitCursor();
  128. }
  129.  
  130.  
  131. /****************** WindowInit ***********************/
  132.  
  133. void     WindowInit( void )
  134. {
  135.     WindowPtr         window;
  136.  
  137.     window = GetNewWindow( kBaseResID, nil, kMoveToFront );
  138.     
  139.     if ( window == nil )
  140.     {
  141.         SysBeep( 10 );    /*  Couldn't load the WIND resource!!! */
  142.         ExitToShell();
  143.     }
  144.     
  145.     SetPort( window );
  146.     TextSize( kClockSize );
  147.     
  148.     gClockRect = window->portRect;
  149.     
  150.     ShowWindow( window );
  151. }
  152.  
  153.  
  154. /****************** MenuBarInit ***********************/
  155.  
  156. void    MenuBarInit( void )
  157. {
  158.     Handle            menuBar;
  159.     MenuHandle        menu;
  160.     ControlHandle    control;
  161.     OSErr            myErr;
  162.     long            feature;
  163.     
  164.     menuBar = GetNewMBar( kBaseResID );
  165.     SetMenuBar( menuBar );
  166.  
  167.     menu = GetMHandle( mApple );
  168.     AddResMenu( menu, 'DRVR' );
  169.     
  170.     menu = GetMenu( mFont );
  171.     InsertMenu( menu, kNotANormalMenu );
  172.     AddResMenu( menu, 'FONT' );
  173.     
  174.     menu = GetMenu( mStyle );
  175.     InsertMenu( menu, kNotANormalMenu );
  176.     CheckItem( menu, iPlain, true );
  177.     
  178.     DrawMenuBar();
  179.  
  180.     HandleFontChoice( gLastFont );
  181.     
  182.     myErr = Gestalt( gestaltPopupAttr, &feature );
  183.     gHasPopupControl = ((myErr == noErr) && (feature & (1 << gestaltPopupPresent)));
  184.     
  185.     if ( gHasPopupControl )
  186.         control = GetNewControl( kPopupControlID, FrontWindow() );
  187. }
  188.  
  189.  
  190. /*************************/
  191.  
  192. void    HandleFontChoice( short item )
  193. {
  194.     short        fontNumber;
  195.     Str255        fontName;
  196.     MenuHandle     menuHandle;
  197.     
  198.     menuHandle = GetMHandle( mFont );
  199.     
  200.     CheckItem( menuHandle, gLastFont, kRemoveCheckMark );
  201.     CheckItem( menuHandle, item, kAddCheckMark );
  202.     
  203.     gLastFont = item;
  204.     
  205.     GetItem( menuHandle, item, fontName );
  206.     GetFNum( fontName, &fontNumber );
  207.     
  208.     TextFont( fontNumber );
  209. }
  210.  
  211.  
  212. /****************** HandleStyleChoice ***********************/
  213.  
  214. void    HandleStyleChoice( short item )
  215. {
  216.     MenuHandle menuHandle;
  217.     
  218.     switch( item )
  219.     {
  220.         case iPlain:
  221.             gCurrentStyle = kPlainStyle;
  222.             break;
  223.         case iBold:
  224.             if ( gCurrentStyle & bold )
  225.                 gCurrentStyle -= bold;
  226.             else
  227.                 gCurrentStyle |= bold;
  228.             break;
  229.         case iItalic:
  230.             if ( gCurrentStyle & italic )
  231.                 gCurrentStyle -= italic;
  232.             else
  233.                 gCurrentStyle |= italic;
  234.             break;
  235.         case iUnderline:
  236.             if ( gCurrentStyle & underline )
  237.                 gCurrentStyle -= underline;
  238.             else
  239.                 gCurrentStyle |= underline;
  240.             break;
  241.         case iOutline:
  242.             if ( gCurrentStyle & outline )
  243.                 gCurrentStyle -= outline;
  244.             else
  245.                 gCurrentStyle |= outline;
  246.             break;
  247.         case iShadow:
  248.             if ( gCurrentStyle & shadow )
  249.                 gCurrentStyle -= shadow;
  250.             else
  251.                 gCurrentStyle |= shadow;
  252.             break;
  253.     }
  254.     
  255.     menuHandle = GetMHandle( mStyle );
  256.     
  257.     CheckItem( menuHandle, iPlain, gCurrentStyle == kPlainStyle );
  258.     CheckItem( menuHandle, iBold, gCurrentStyle & bold );
  259.     CheckItem( menuHandle, iItalic, gCurrentStyle & italic );
  260.     CheckItem( menuHandle, iUnderline, gCurrentStyle & underline );
  261.     CheckItem( menuHandle, iOutline, gCurrentStyle & outline );
  262.     CheckItem( menuHandle, iShadow, gCurrentStyle & shadow );
  263.     
  264.     TextFace( gCurrentStyle );
  265. }
  266.  
  267.  
  268. /****************** DoUpdate ***********************/
  269.  
  270. void    DoUpdate( EventRecord *eventPtr )
  271. {
  272.     WindowPtr        window;
  273.     Str255            timeString;
  274.     unsigned long    curTimeInSecs;
  275.     
  276.     window = (WindowPtr)eventPtr->message;
  277.     
  278.     BeginUpdate( window );
  279.     
  280.     GetDateTime ( &curTimeInSecs );
  281.     curTimeInSecs += GetZoneOffset();
  282.     
  283.     IUTimeString( (long)curTimeInSecs, kIncludeSeconds,
  284.             timeString );
  285.     
  286.     EraseRect( &gClockRect );
  287.     MoveTo( kClockLeft, kClockTop );
  288.     DrawString( timeString );
  289.     
  290.     DrawControls( window );
  291.     
  292.     EndUpdate( window );
  293. }
  294.  
  295.  
  296. /****************** GetZoneOffset ***********************/
  297.  
  298. long GetZoneOffset( void )
  299. {
  300.     MachineLocation    loc;
  301.     long            delta, defaultZoneOffset;
  302.     
  303.     ReadLocation( &loc );
  304.     defaultZoneOffset = ClearHighByte( loc.gmtFlags.gmtDelta );
  305.     
  306.     if ( IsHighBitSet( defaultZoneOffset ) )
  307.         SetHighByte( defaultZoneOffset );
  308.         
  309.     switch ( gCurrentZoneID )
  310.     {
  311.         case kCurrentTimeZone :
  312.             delta = defaultZoneOffset;
  313.             break;
  314.         case kNewYorkTimeZone :
  315.             delta = -5L * kSecondsPerHour ;
  316.             break;
  317.         case kMoscowTimeZone :
  318.             delta = 3L * kSecondsPerHour;
  319.             break;
  320.         case kUlanBatorTimeZone :
  321.             delta = 8L * kSecondsPerHour;
  322.             break;
  323.     }
  324.     delta -= defaultZoneOffset;
  325.     
  326.     return delta;
  327. }